package com.example.myweb.Domain.Entity;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.Collections; // 为了简化，我们暂时只分配一个默认角色
import java.util.List;

@Data
@Entity
@Table(name = "users") // 假设你的表名是 'users'
public class User implements UserDetails {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long userId;

    @Column(unique = true, nullable = false)
    private String email;


    @Column(name = "password_hash", nullable = false) // 明确映射到数据库的 password_hash 列
    private String passwordHash;

    @Column(unique = true)
    private String phoneNumber;

    @Column(nullable = false, length = 50)
    private String nickname;

    @Column(length = 255)
    private String avatarUrl;

    @Column(length = 500)
    private String bio;

    @Column(nullable = false, updatable = false) // 注册时间不应被更新
    private LocalDateTime registrationTime;

    @Column(name = "status", nullable = false)
    private UserStatus status;

    @Column(nullable = false, length = 20)
    private String role; // 例如: "USER", "ADMIN"



    @Column(name = "reset_token") // 用于密码重置令牌
    private String resetToken;

    @Column(name = "reset_token_expiry_time") // 令牌过期时间
    private LocalDateTime resetTokenExpiryTime;

    private LocalDateTime lastLoginTime;
//    @Transient
//    private Long followingCount;
//
//    @Transient
//    private Long followerCount;

    public enum UserStatus {
        NORMAL, // 正常
        MUTED,  // 禁言
        BANNED  // 封禁
    }



    // 如果需要构造函数来接收JPQL查询结果，可以添加一个
    public User(Long userId, String nickname, String avatarUrl, Long followingCount, Long followerCount) {
        this.userId = userId;
        this.nickname = nickname;
        this.avatarUrl = avatarUrl;
//        this.followingCount = followingCount;
//        this.followerCount = followerCount;
    }

    // 确保有一个无参构造函数供JPA使用


    // 默认构造函数 (JPA 要求)
    public User() {
        this.registrationTime = LocalDateTime.now();
        this.status = UserStatus.NORMAL;
        this.role = "USER"; // <<< 默认新注册用户是 USER 角色
    }


    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    // !!! 核心修改在这里 !!!
    // 注意：UserDetails 接口要求实现 getPassword() 方法，它应该返回哈希后的密码。
    // 所以这里的 getPassword() 应该返回 passwordHash。



    @Override
    public String getPassword() {
        return passwordHash;
    }

    // !!! 新增 setPasswordHash 方法 !!!
    public void setPasswordHash(String passwordHash) {
        this.passwordHash = passwordHash;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public String getNickname() {
        return nickname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }

    public String getAvatarUrl() {
        return avatarUrl;
    }

    public void setAvatarUrl(String avatarUrl) {
        this.avatarUrl = avatarUrl;
    }

    public String getBio() {
        return bio;
    }

    public void setBio(String bio) {
        this.bio = bio;
    }

    public LocalDateTime getRegistrationTime() {
        return registrationTime;
    }

    public void setRegistrationTime(LocalDateTime registrationTime) {
        this.registrationTime = registrationTime;
    }



    public String getResetToken() {
        return resetToken;
    }

    public void setResetToken(String resetToken) {
        this.resetToken = resetToken;
    }

    public LocalDateTime getResetTokenExpiryTime() {
        return resetTokenExpiryTime;
    }

    public void setResetTokenExpiryTime(LocalDateTime resetTokenExpiryTime) {
        this.resetTokenExpiryTime = resetTokenExpiryTime;
    }

    // --- UserDetails 接口实现 ---
//    @Override
//    public Collection<? extends GrantedAuthority> getAuthorities() {
//        // 返回用户的权限/角色列表。目前可以返回空列表或默认角色
//        return Collections.emptyList(); // 示例：目前没有定义角色，返回空列表
//    }
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        // 根据 role 字段，动态创建权限列表
        // 注意：Spring Security的hasRole()会自动添加 "ROLE_" 前缀
        // 所以我们在这里创建的权限对象也应该是 "ROLE_ADMIN", "ROLE_USER"
        if (this.role == null) {
            return Collections.emptyList();
        }
        return List.of(new SimpleGrantedAuthority("ROLE_" + this.role));
    }

    @Override
    public String getUsername() {
        // 通常返回用于认证的唯一标识，这里使用 email
        return this.email;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return "NORMAL".equals(this.status.name()); // 使用枚举常量名进行比较
    }
}